<?php

namespace app\beautiful\controller\admin\setting;

use app\BaseController;
use app\beautiful\model\setting\ConsumeBonusModel;
use app\beautiful\model\setting\ConsumeBonusSettingModel;
use think\facade\Db;


class ConsumeBonus extends BaseController{
    public function openSet(){
        $is_open = $this->request->param('is_open/d',0);
        ConsumeBonusSettingModel::where('shop_id',$this->request->shopId)->where(['is_delete'=>0])->save(['is_open'=>$is_open]);
        return $this->success('设置成功');
    }

    public function showConsumeBonusSet(){
        if(null === $setting = ConsumeBonusSettingModel::where('shop_id',$this->request->shopId)->where(['is_delete'=>0])->find()){

            $setting = new ConsumeBonusSettingModel();
            $setting->shop_id = $this->request->shopId;
            $setting->bonus_budget_type = 1;
            $setting->bonus_budget = 0;
            $setting->buy_bonus_budget = 0;
            $setting->normal_bonus_budget = 0;
            $setting->bonus_cycle = 1;
            $setting->bonus_cycle_type = 1;
            $setting->day_rank_ratio = 0;
            $setting->week_rank_ratio = 0;
            $setting->month_rank_ratio = 0;
            $setting->is_open = 0;
            $setting->save();
        }
        $return['detail']=$setting;

        return $this->success($return);
    }

    public function saveConsumeBonusSet(){
        $bonus_budget_type = $this->request->param('bonus_budget_type/d',1);
        $bonus_budget = $this->request->param('bonus_budget/f',0);
        $buy_bonus_budget = $this->request->param('buy_bonus_budget/f',0);
        $normal_bonus_budget = $this->request->param('normal_bonus_budget/f',0);
        $bonus_cycle = $this->request->param('bonus_cycle/d',1);
        $bonus_cycle_type = $this->request->param('bonus_cycle_type/d',1);
        $day_rank_ratio = $this->request->param('day_rank_ratio/f',0);
        $week_rank_ratio = $this->request->param('week_rank_ratio/f',0);
        $month_rank_ratio = $this->request->param('month_rank_ratio/f',0);

        if(null === $setting = ConsumeBonusSettingModel::where('shop_id',$this->request->shopId)->where(['is_delete'=>0])->find()){

            $setting = new ConsumeBonusSettingModel();
            $setting->shop_id = $this->request->shopId;
        }

        Db::startTrans();

        try{
            if($bonus_budget_type==1){
                $setting->bonus_budget = $bonus_budget;
            }else{
                $setting->buy_bonus_budget = $buy_bonus_budget;
                $setting->normal_bonus_budget = $normal_bonus_budget;
            }

            $setting->bonus_budget_type = $bonus_budget_type;
            $setting->bonus_cycle = $bonus_cycle;
            $setting->bonus_cycle_type = $bonus_cycle_type;
            $setting->day_rank_ratio = $day_rank_ratio;
            $setting->week_rank_ratio = $week_rank_ratio;
            $setting->month_rank_ratio = $month_rank_ratio;
            $setting->save();
            //如果当前门店没有生成消费分红数据，则生成消费分红的第一条数据（只需要第一次设置时生成，后面计划消费分红后在根据最新的分红设置生成下一条分红数据）（分红时除周期外 其他以最新设置为准）
            //$this->addConsumeBonusData($setting);//暂时不用这种方法
            Db::commit();

        }catch(\Exception $e){

            Db::rollback();

            return $this->error($e->getMessage());

        }
        return $this->success('操作成功');
    }
    protected function addConsumeBonusData($consume_bonus_setting){

        if(null === $consume_bonus =ConsumeBonusModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0])->find()){

            $start_time = date('Y-m-d H:i:s');

            $consumeBonusModel = new ConsumeBonusModel();
            $bonus_cycle_type = $consume_bonus_setting->bonus_cycle_type;
            if($consume_bonus_setting->bonus_cycle == 1){

                $add['shop_id'] = $this->request->shopId;
                $add['num'] = 1;

                //单次分红
                if($bonus_cycle_type==1){
                    //周
                    //开始时间
                    $start_time = mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y'));

                    $time_type = 1;
                    $end_time = mktime(23,59,59,date('m'),date('d')-date('w')+7,date('y'));

                    $add['week_rank_ratio'] = $consume_bonus_setting->week_rank_ratio;
                }else if($bonus_cycle_type==2){
                    //月
                    //开始时间
                    $start_time = mktime(0,0,0,date('m'),1,date('Y'));

                    $time_type = 2;
                    $end_time = mktime(23,59,59,date('m'),date('t'),date('Y'));
                    $add['month_rank_ratio'] = $consume_bonus_setting->month_rank_ratio;
                }else{
                    //季度
                    //获取当前季度
                    $time_type = 3;
                    $season = ceil((date('m'))/3);

                    //当前季度开始时间戳
                    $start_time  = mktime(0, 0, 0,$season*3-3+1,1,date('Y'));

                    $end_time = mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'));
                    $add['quarter_rank_ratio'] = $consume_bonus_setting->quarter_rank_ratio;
                }

                $start_time = date('Y-m-d H:i:s', $start_time);
                $end_time = date('Y-m-d', $end_time).' 23:59:59';

                $add['start_time'] = $start_time;
                $add['end_time'] = $end_time;
                $add['time_type'] = $time_type;
                $add['bonus_budget_type'] = $consume_bonus_setting->bonus_budget_type;
                $add['buy_bonus_budget'] = $consume_bonus_setting->buy_bonus_budget;
                $add['normal_bonus_budget'] = $consume_bonus_setting->normal_bonus_budget;
                $add['bonus_budget'] = $consume_bonus_setting->bonus_budget;
                $consume_bonus_add[] = $add;
            }else{
                //两次分红
                if($bonus_cycle_type==1){
                    //周+月
                    //本周结束时间
                    $time_type = 1;
                    $start_time = mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y'));
                    $week_end_time = mktime(23,59,59,date('m'),date('d')-date('w')+7,date('y'));
                    $consume_bonus_add[]=[
                        'shop_id'=>$this->request->shopId,
                        'start_time'=>date('Y-m-d H:i:s',$start_time),
                        'end_time'=> date('Y-m-d', $week_end_time).' 23:59:59',
                        'num'=>1,
                        'time_type'=>$time_type,
                        'week_rank_ratio'=>$consume_bonus_setting->week_rank_ratio,
                        'bonus_budget_type' => $consume_bonus_setting->bonus_budget_type,
                        'buy_bonus_budget' => $consume_bonus_setting->buy_bonus_budget,
                        'normal_bonus_budget' => $consume_bonus_setting->normal_bonus_budget,
                        'bonus_budget' => $consume_bonus_setting->bonus_budget
                    ];
                    //本月结束时间
                    $time_type = 2;
                    $start_time = mktime(0,0,0,date('m'),1,date('Y'));
                    $month_end_time = mktime(23,59,59,date('m'),date('t'),date('Y'));
                    $consume_bonus_add[]=[
                        'shop_id'=>$this->request->shopId,
                        'start_time'=>date('Y-m-d H:i:s',$start_time),
                        'end_time'=>date('Y-m-d', $month_end_time).' 23:59:59',
                        'num'=>2,
                        'time_type'=>$time_type,
                        'month_rank_ratio'=>$consume_bonus_setting->month_rank_ratio,
                        'bonus_budget_type' => $consume_bonus_setting->bonus_budget_type,
                        'buy_bonus_budget' => $consume_bonus_setting->buy_bonus_budget,
                        'normal_bonus_budget' => $consume_bonus_setting->normal_bonus_budget,
                        'bonus_budget' => $consume_bonus_setting->bonus_budget
                    ];
                }else if($bonus_cycle_type==2){
                    //月+季度
                    //本月结束时间
                    $time_type = 2;
                    $start_time = mktime(0,0,0,date('m'),1,date('Y'));
                    $month_end_time =mktime(23,59,59,date('m'),date('t'),date('Y'));
                    $consume_bonus_add[]=[
                        'shop_id'=>$this->request->shopId,
                        'start_time'=>date('Y-m-d H:i:s',$start_time),
                        'end_time'=>date('Y-m-d', $month_end_time).' 23:59:59',
                        'num'=>1,
                        'time_type'=>$time_type,
                        'month_rank_ratio'=>$consume_bonus_setting->month_rank_ratio,
                        'bonus_budget_type' => $consume_bonus_setting->bonus_budget_type,
                        'buy_bonus_budget' => $consume_bonus_setting->buy_bonus_budget,
                        'normal_bonus_budget' => $consume_bonus_setting->normal_bonus_budget,
                        'bonus_budget' => $consume_bonus_setting->bonus_budget
                    ];
                    //本季度结束时间
                    $time_type = 3;
                    $season = ceil((date('m'))/3);
                    $start_time = mktime(0, 0, 0,$season*3-3+1,1,date('Y'));
                    $jd_end_time = mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'));
                    $consume_bonus_add[]=[
                        'shop_id'=>$this->request->shopId,
                        'start_time'=>date('Y-m-d H:i:s',$start_time),
                        'end_time'=>date('Y-m-d', $jd_end_time).' 23:59:59',
                        'num'=>2,
                        'time_type'=>$time_type,
                        'quarter_rank_ratio'=>$consume_bonus_setting->quarter_rank_ratio,
                        'bonus_budget_type' => $consume_bonus_setting->bonus_budget_type,
                        'buy_bonus_budget' => $consume_bonus_setting->buy_bonus_budget,
                        'normal_bonus_budget' => $consume_bonus_setting->normal_bonus_budget,
                        'bonus_budget' => $consume_bonus_setting->bonus_budget
                    ];
                }else if($bonus_cycle_type==3){
                    //月+半年
                    //本月结束时间
                    $time_type = 2;
                    $start_time = mktime(0,0,0,date('m'),1,date('Y'));
                    $month_end_time =mktime(23,59,59,date('m'),date('t'),date('Y'));
                    $consume_bonus_add[]=[
                        'shop_id'=>$this->request->shopId,
                        'start_time'=>date('Y-m-d H:i:s',$start_time),
                        'end_time'=>date('Y-m-d', $month_end_time).' 23:59:59',
                        'num'=>1,
                        'time_type'=>$time_type,
                        'month_rank_ratio'=>$consume_bonus_setting->month_rank_ratio,
                        'bonus_budget_type' => $consume_bonus_setting->bonus_budget_type,
                        'buy_bonus_budget' => $consume_bonus_setting->buy_bonus_budget,
                        'normal_bonus_budget' => $consume_bonus_setting->normal_bonus_budget,
                        'bonus_budget' => $consume_bonus_setting->bonus_budget
                    ];
                    //半年结束时间
                    $time_type = 4;
                    if(date('m')<=6){
                        //上半年开始时间
                        $start_time = strtotime(date('Y').'-01-01 00:00:00');
                        //上半年结束时间
                        $year_end_time =mktime(23,59,59,6,1,date('Y'));
                    }else{
                        //下半年开始时间
                        $start_time = strtotime(date('Y').'-07-01 00:00:00');
                        //下半年结束时间
                        $year_end_time =mktime(23,59,59,12,1,date('Y'));
                    }

                    $consume_bonus_add[]=[
                        'shop_id'=>$this->request->shopId,
                        'start_time'=>date('Y-m-d H:i:s',$start_time),
                        'end_time'=>date('Y-m-t', $year_end_time).' 23:59:59',
                        'num'=>2,
                        'time_type'=>$time_type,
                        'half_year_rank_ratio'=>$consume_bonus_setting->half_year_rank_ratio,
                        'bonus_budget_type' => $consume_bonus_setting->bonus_budget_type,
                        'buy_bonus_budget' => $consume_bonus_setting->buy_bonus_budget,
                        'normal_bonus_budget' => $consume_bonus_setting->normal_bonus_budget,
                        'bonus_budget' => $consume_bonus_setting->bonus_budget
                    ];
                }else if($bonus_cycle_type==4){
                    //季度+半年
                    //本季度结束时间
                    $time_type = 3;
                    $season = ceil((date('m'))/3);
                    $start_time = mktime(0, 0, 0,$season*3-3+1,1,date('Y'));
                    $jd_end_time = mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'));
                    $consume_bonus_add[]=[
                        'shop_id'=>$this->request->shopId,
                        'start_time'=>date('Y-m-d H:i:s',$start_time),
                        'end_time'=>date('Y-m-d', $jd_end_time).' 23:59:59',
                        'num'=>1,
                        'time_type'=>$time_type,
                        'quarter_rank_ratio'=>$consume_bonus_setting->quarter_rank_ratio,
                        'bonus_budget_type' => $consume_bonus_setting->bonus_budget_type,
                        'buy_bonus_budget' => $consume_bonus_setting->buy_bonus_budget,
                        'normal_bonus_budget' => $consume_bonus_setting->normal_bonus_budget,
                        'bonus_budget' => $consume_bonus_setting->bonus_budget
                    ];

                    //半年结束时间
                    $time_type = 4;
                    if(date('m')<=6){
                        //上半年开始时间
                        $start_time = strtotime(date('Y').'-01-01 00:00:00');
                        //上半年结束时间
                        $year_end_time =mktime(23,59,59,6,1,date('Y'));
                    }else{
                        //下半年开始时间
                        $start_time = strtotime(date('Y').'-07-01 00:00:00');
                        //下半年结束时间
                        $year_end_time =mktime(23,59,59,12,1,date('Y'));
                    }

                    $consume_bonus_add[]=[
                        'shop_id'=>$this->request->shopId,
                        'start_time'=>date('Y-m-d H:i:s',$start_time),
                        'end_time'=>date('Y-m-t', $year_end_time).' 23:59:59',
                        'num'=>2,
                        'time_type'=>$time_type,
                        'half_year_rank_ratio'=>$consume_bonus_setting->half_year_rank_ratio,
                        'bonus_budget_type' => $consume_bonus_setting->bonus_budget_type,
                        'buy_bonus_budget' => $consume_bonus_setting->buy_bonus_budget,
                        'normal_bonus_budget' => $consume_bonus_setting->normal_bonus_budget,
                        'bonus_budget' => $consume_bonus_setting->bonus_budget
                    ];
                }else{
                    //季度+年
                    //本季度结束时间
                    $time_type = 3;
                    $season = ceil((date('m'))/3);
                    $start_time = mktime(0, 0, 0,$season*3-3+1,1,date('Y'));
                    $jd_end_time = mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'));
                    $consume_bonus_add[]=[
                        'shop_id'=>$this->request->shopId,
                        'start_time'=>date('Y-m-d H:i:s',$start_time),
                        'end_time'=>date('Y-m-d', $jd_end_time).' 23:59:59',
                        'num'=>1,
                        'time_type'=>$time_type,
                        'quarter_rank_ratio'=>$consume_bonus_setting->quarter_rank_ratio,
                        'bonus_budget_type' => $consume_bonus_setting->bonus_budget_type,
                        'buy_bonus_budget' => $consume_bonus_setting->buy_bonus_budget,
                        'normal_bonus_budget' => $consume_bonus_setting->normal_bonus_budget,
                        'bonus_budget' => $consume_bonus_setting->bonus_budget
                    ];

                    //年结束时间
                    $time_type = 5;
                    $start_time = strtotime(date('Y').'-01-01 00:00:00');
                    $year_end_time = mktime(23,59,59,12,1,date('Y'));
                    $consume_bonus_add[]=[
                        'shop_id'=>$this->request->shopId,
                        'start_time'=>date('Y-m-d H:i:s',$start_time),
                        'end_time'=>date('Y-m-t', $year_end_time).' 23:59:59',
                        'num'=>1,
                        'time_type'=>$time_type,
                        'year_rank_ratio'=>$consume_bonus_setting->year_rank_ratio,
                        'bonus_budget_type' => $consume_bonus_setting->bonus_budget_type,
                        'buy_bonus_budget' => $consume_bonus_setting->buy_bonus_budget,
                        'normal_bonus_budget' => $consume_bonus_setting->normal_bonus_budget,
                        'bonus_budget' => $consume_bonus_setting->bonus_budget
                    ];
                }
            }
            $consumeBonusModel->saveAll($consume_bonus_add);
        }


    }




}

